<?php
/**
 * 用户登录注册
 */

namespace App\Controller\Member;

use Common\Controller\App\SignController;
use Common\Model\CaptchaModel;
use Common\Model\MemberModel;
use Common\Model\MemberOtherPlatformModel;
use Common\Model\MicroShopModel;
use Common\Model\TarentoModel;

class LoginController extends SignController{

    /**
     * 注册
     */
    public function register(){
        $shop_id = self::$shop_id;
        $mobile = I('post.mobile');
        $code = I('post.code');
        if(!preg_match("/^1[34578]\d{9}$/", $mobile)){
            $this->ajaxReturn(['code'=>300,'msg'=>'请输入正确的手机号码']);
        }
        if(empty($code)){
            $this->ajaxReturn(['code'=>300,'msg'=>'请输入手机验证码']);
        }
        $model = new CaptchaModel();
        $result = $model->where("unique_str='$mobile' and captcha='$code'")->order('id desc')->find();
        $model->where("unique_str=$mobile")->delete();
        if(empty($result)){
            $this->ajaxReturn(['code'=>300,'msg'=>'手机验证码输入错误']);
        }
        if(time()>strtotime($result['invalid_time'])){
            $this->ajaxReturn(['code'=>300,'msg'=>'手机验证码已过期，请重新发送']);
        }
        $model = new MemberModel();;
        $info = $model->where("mobile='$mobile'")->field("userid")->find();
        if($info){
            session('member_id',$info['userid']);
            $model->where("mobile='$mobile'")->save(['lastlogintime'=>time()]);
            $this->ajaxReturn(['code'=>200,'msg'=>'登录成功']);
        }else{
            M()->startTrans();
            $t_model = new TarentoModel();
            $s_model = new MicroShopModel();
            $m_model = new MemberOtherPlatformModel();
            $data['name'] = 'Y'.$mobile;
            $data['mobile'] = $mobile;
            $data['lastlogintime'] = time();
            $data['client_platform'] = 4;
            $data['client_system'] = 3;
            $data['create_time'] = date('Y-m-d H:i:s',time());
            $m_id = $model->add($data);
            $fields = ['member_id','relations','direct_rec_mid', 'indirect_rec_mid', 'level','account', 'have_micro_shop','have_discount_price','invite_code'];
            $tarentInfo = $t_model->where("member_id=$shop_id")->field($fields)->find();

            if (!empty($tarentInfo) && !$tarentInfo['have_micro_shop'] && $tarentInfo['relations'] != '') {
                //没店铺，则使用推广的上一级
                $relations = explode(',', $tarentInfo['relations']);
                foreach ($relations as $k => $v) {
                    if ($v == $shop_id) {
                        $belongToShopId = $relations[$k - 1];
                    }
                }
                $tarentInfo = $t_model->where("member_id=$belongToShopId")->field($fields)->find();
            }

            if (empty($tarentInfo) || !$tarentInfo['have_micro_shop']) {
                //如果没查到tarento数据，或没开店，或上一级也没有开店，原则上不是存在的，由于可能是人为在URL输入，这里写成官方的
                $belongToShopId = 14441;
                $tarentInfo = $t_model->where("member_id=$belongToShopId")->field($fields)->find();
            }

            $newTarent['direct_rec_mid']   = $belongToShopId;
            $newTarent['member_id']        = $m_id;
            $newTarent['have_micro_shop']  = 1;
            $newTarent['can_upgrade_shop'] = 1;
            $newTarent['indirect_rec_mid'] = $tarentInfo->direct_rec_mid;
            $newTarent['relations']        = $tarentInfo['relations'].$m_id.',';
            $newTarent['level'] = 3;
            $t_id = $t_model->add($newTarent);

            $s_data['create_time'] = date('Y-m-d H:i:s',time());
            $s_data['status']      = 1;
            $s_data['level']       = 3;
            $s_data['member_id']   = $m_id;
            $s_data['name']        = 'Y'.$mobile;
            $s_id = $s_model->add($s_data);
            $m_data['userid']       = $m_id;
            $m_data['open_type']    = 1;
            $m_data['name']         = 'Y'.$mobile;
            $m_data['client_platform']  = 1;
            $m_data['client_system']  = 1;
            $m_data['create_time']  = date('Y-m-d H:i:s',time());
            $m_data['last_login_time']  = date('Y-m-d H:i:s',time());

            $mt_id = $m_model->add($m_data);
            if($mt_id && $s_id && $t_id && $m_id){
                M()->commit();
                session('member_id',$m_id);
                $this->ajaxReturn(['code'=>200,'msg'=>'登录成功']);
            }else{
                M()->rollback();
                $this->ajaxReturn(['code'=>300,'msg'=>'登录失败，请稍后再试！']);
            }
        }
    }
}